<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Nsearch extends Base{
    //资讯视频混合搜索
    public function pnvsearch(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.key"))){
            $key=input('param.key');
        }else{
            return json(['code' => 503, 'msg' => '搜索关键词呢']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $news=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                $products=Db::name('nvideo')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products||$news){
                    for ($i=0; $i <count($news) ; $i++) { 
                        $news[$i]['times']=date('Y-m-d',$news[$i]['uptime']);
                        $news[$i]['agree']=Db::name('nagree')->where('cus_id',$cus_id)->where('type',1)->where('p_id',$news[$i]['id'])->count();
                        $news[$i]['message']=Db::name('nmessage')->where('cus_id',$cus_id)->where('type',1)->where('p_id',$news[$i]['id'])->count();
                        $news[$i]['type']=1;
                    }
                    for ($i=0; $i <count($products) ; $i++) { 
                        $products[$i]['times']=date('Y-m-d',$products[$i]['uptime']);
                        $products[$i]['agree']=Db::name('nagree')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$products[$i]['id'])->count();
                        $products[$i]['message']=Db::name('nmessage')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$products[$i]['id'])->count();
                        $products[$i]['type']=2;
                        array_push($news,$products[$i]);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$news]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $news=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                $products=Db::name('nvideo')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products||$news){
                    for ($i=0; $i <count($news) ; $i++) { 
                        $news[$i]['times']=date('Y-m-d',$news[$i]['uptime']);
                        $news[$i]['agree']=Db::name('nagree')->where('cus_id',$cus_id)->where('type',1)->where('p_id',$news[$i]['id'])->count();
                        $news[$i]['message']=Db::name('nmessage')->where('cus_id',$cus_id)->where('type',1)->where('p_id',$news[$i]['id'])->count();
                        $news[$i]['type']=1;
                    }
                    for ($i=0; $i <count($products) ; $i++) { 
                        $products[$i]['times']=date('Y-m-d',$products[$i]['uptime']);
                        $products[$i]['agree']=Db::name('nagree')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$products[$i]['id'])->count();
                        $products[$i]['message']=Db::name('nmessage')->where('cus_id',$cus_id)->where('type',2)->where('p_id',$products[$i]['id'])->count();
                        $products[$i]['type']=2;
                        array_push($news,$products[$i]);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$news]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
}